
using Xeralabs.GDESK.Entities;
using Xeralabs.GDESK.Events;
using Xeralabs.GDESK.Events.Handling;
using Xeralabs.GDESK.Utils;
using System;
using System.Threading;

namespace Xeralabs.GDESK.Test.Entities
{
    public delegate void Test02Delegate(ulong sender, ulong reciver, double timeMark);

    public class Test02Entity : XGDESKEntity
    {
        private static int _numChars0_25 = 0;
        private static int _numNPCS25_50 = 0;
        private static int _numNoUpdatables50_90 = 0;
        private static int _numUpdatables90_100 = 0;
        private static int _completed = 0;

#if DEBUG
        public static void InfoMe()
        {
            System.Diagnostics.Debug.WriteLine(String.Format("Objetos tipo 'Elemento Activo':\t\t{0}", _numChars0_25));
            //System.Diagnostics.Debug.WriteLine(String.Format("Objetos tipo 'Personaje Inactivo':\t\t{0}", _numNPCS25_50));
            System.Diagnostics.Debug.WriteLine(String.Format("Objetos tipo 'Elemento Inactivo':\t\t{0}", _numNPCS25_50+_numNoUpdatables50_90));
            System.Diagnostics.Debug.WriteLine(String.Format("Objetos tipo 'Elemento Interactor':\t\t{0}", _numUpdatables90_100));
        }
#endif

        private bool _demo = false;
        private bool _updateEvent = false;
        private double _res = 0.0;
        private Random _rndTime = null;
        public event Test02Delegate UpdateForm = null;

        public Test02Entity(byte objectType, byte reserved)
            : base(objectType, reserved)
        {
            //DO NOTHING POR AHORA
            _rndTime = new Random();
        }
        public override void ProcessInteraction(XGDESKEvent evt)
        {
            //DO NOTHING POR AHORA
            if (UpdateForm != null)
            {
                this.UpdateForm(evt.Sender, evt.Reciver, evt.TimeMark);
            }
            if (_demo)
            {
                _updateEvent = (evt.Sender == GetGUID());
                this.DoInitialEvents();
            }
        }

        public override void InteractionRevoked(XGDESKEvent evt)
        {
            //DO NOTHING POR AHORA

        }

        public override void DoInitialEvents()
        {
            if (!_demo)
            {
                if (_completed == 0)
                    _completed++;

                if (_numNoUpdatables50_90 < (40 * _completed))
                {
                    _numNoUpdatables50_90++;
                    this._res = 0.75;
                }
                else if (_numUpdatables90_100 < (10 * _completed))
                {
                    _numUpdatables90_100++;
                    this._res = 0.95;
                }
                else if (_numNPCS25_50 < (25 * _completed))
                {
                    _numNPCS25_50++;
                    this._res = 0.4;
                }
                else if (_numChars0_25 < (25 * _completed))
                {
                    _numChars0_25++;
                    this._res = 0.2;
                }

                if (_numChars0_25 == (25 * _completed))
                    _completed++;

                _demo = true;
                _updateEvent = true;
            }
            if (_demo)
            {
                if (_res < 0.25)
                {
                    if (_updateEvent)
                    {
                        this.SendInteraction(GetGUID(), 0.5, null);
                    }
                }
                else if (_res > 0.90)
                {
                    if (_updateEvent)
                    {
                        this.SendInteraction(GetGUID(), 10.0, null);
                    }
                }
            }
        }

        public override void StopEntity(bool save)
        {
            //DO NOTHING POR AHORA

        }

        internal void Send(ulong reciver, double delay, iXGDESKEventArgs args)
        {
            this.SendInteraction(reciver, delay, args);
        }
    }
}
